<?php

	//檢查Facebook Uid是否存在
	function fn_checkUserAccExist($uid){
		global $_db;
		$sql = "SELECT uid FROM aams_user WHERE uid='$uid'";
		return $_db->GetOne($sql);	
	}
	
	//儲存會員登入資料
	function fn_saveLoginAuthSession($type, $uid){
		global $_db, $config;
		$sql = "SELECT uid, nickname FROM aams_user WHERE uid='$uid'";
		$aryData = $_db->GetRow($sql);
		if($aryData)
		{
			$_SESSION['LoginAuth']['type'] = $type;
			$_SESSION['LoginAuth']['uid'] = $aryData['uid'];
			$_SESSION['LoginAuth']['utype'] = $aryData['type'];
			$_SESSION['LoginAuth']['nickname'] = $aryData['nickname'];
			return fn_updateLoginTime($uid);		
		}else
			return false;
	}
	
	//更新會員登入時間
	function fn_updateLoginTime($uid){
		global $_db, $config;
		$sql = "UPDATE aams_user SET login_time=NOW() WHERE uid='$uid'";
		return $_db->Execute($sql);
	}
	//存取Facebook 粉絲團頁面
	function fn_loadFacebookPages($uid){
		global $facebook;
		$pages = $facebook->api("/$uid/accounts");
		if( sizeof($pages['data']) > 0 )
		{
			foreach( $pages['data'] as $k => $v )
			{
				if( $v['category'] != 'Application' )	
				{
					$pages_ary['name'] = $v['name'];
					$pages_ary['id'] = $v['id'];
					$pages_list[] = $pages_ary;
				}
			}
		}
		return (sizeof($pages_list) > 0)? $pages_list:false;
	}
	
	
	function fn_set_notification($type, $message, $url='')
	{
	
		if (empty($_SESSION['notifications'])) {
			$_SESSION['notifications'] = array();
		}
	
		$key = md5($type . $message . $url);
	
		$_SESSION['notifications'][$key] = array(
			'type' => $type,
			'message' => $message,
			'url' => $url
		);
	
		if( empty($url) )
		{
			$url = 'javascript:history.go(-1)';
		}else
			$url = 'window.location.href="'.$url.'"';
		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
		echo '<script language="JAVASCRIPT">
					'.$url.';</script>';
		exit;
	}	
	
	function fn_get_notification(){
		if (empty($_SESSION['notifications'])) {
			$_SESSION['notifications'] = array();
		}
	
		$_notifications = array();
		foreach ($_SESSION['notifications'] as $k => $v) {
			$v['key'] = $k;
			Registry::get('tpl')->assign("notifications", $v);
			$_notifications = Registry::get('tpl')->fetch("dashboard/notifications.html");
			unset($_SESSION['notifications'][$k]);
		}
	
		return $_notifications;		
	}
?>

